/*
    Base, version 1.0.1
    Copyright 2006, Dean Edwards
    License: http://creativecommons.org/licenses/LGPL/2.1/
*/
;(function(window) {
    window.Base = function() {};
    window.Base.prototype = {
        extend: function(source, value) {
            var extend = Base.prototype.extend;
            //参数大于一个时 
            if (arguments.length == 2) {
                //获得proto的祖先 
                var ancestor = this[source];
                //如果value是function，并且祖先对象存在，在重载函数中调用base时 
                //&& 祖先与value的方法内容不一致 && value中没有的精确的‘inherit’ 字眼
                if ((ancestor instanceof Function) && (value instanceof Function) &&
                    ancestor.valueOf() != value.valueOf() && /\binherit\b/.test(value)) {
                    var method = value;
                    value = function() {
                        var previous = this.inherit;
                        this.inherit = ancestor;
                        var returnValue = method.apply(this, arguments);
                        this.inherit = previous;
                        return returnValue;
                    };
                    // // point to the underlying method
                    // value.valueOf = function() {
                    //  return method;
                    // };
                    // value.toString = function() {
                    //  return String(method);
                    // };
                }
                return this[source] = value;
            } else if (source) {
                var _prototype = { toSource: null };
                // do the "toString" and other methods manually
                var _protected = ["toString", "valueOf"];
                // if we are prototyping then include the constructor
                if (Base._prototyping) _protected[2] = "constructor";
                for (var i = 0;
                    (name = _protected[i]); i++) {
                    if (source[name] != _prototype[name]) {
                        extend.call(this, name, source[name]);
                    }
                }
                // copy each of the source object's properties to this object
                for (var name in source) {
                    if (!_prototype[name]) {
                        extend.call(this, name, source[name]);
                    }
                }
            }
            return this;
        },
        inherit: function() {
            // call this method from any other method to invoke that method's ancestor
        }
    };
    window.Base.extend = function(_instance, _static) {
        var extend = Base.prototype.extend;
        if (!_instance) _instance = {};
        // 创建原型,设置原型标志
        Base._prototyping = true;
        var _prototype = new this;
        extend.call(_prototype, _instance);


        var constructor = _prototype.constructor;
        _prototype.constructor = this;

        //类实例属性和方法的原型部分构造完毕,删除标志位
        delete Base._prototyping;
        // create the wrapper for the constructor function
        var klass = function() {
            if (!Base._prototyping) constructor.apply(this, arguments);
            this.constructor = klass;
        };

        klass.prototype = _prototype;
        for (var key in _prototype) { //处理对象和数组
            if ((_prototype[key] instanceof Object) && (!_prototype[key] instanceof Function)) {
                klass[key] = _prototype[key];
            }
        }
        // build the class interface
        klass.extend = this.extend;
        klass.toString = function() {
            return String(constructor);
        };
        extend.call(klass, _static);
        // support singletons
        var object = constructor ? klass : _prototype;
        // class initialisation
        if (object.init instanceof Function) object.init();
        return object;
    };

    Function.prototype.bindAsEventListener = function(object) {
        var __method = this; //指代调用该方法的function
        return function(event) {
            return __method.call(object, event || window.event);
        }
    }

    function IsPC() {
        var userAgentInfo = navigator.userAgent;
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
        var flag = true;
        for (var v = 0; v < Agents.length; v++) {
            if (userAgentInfo.indexOf(Agents[v]) > 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }
})(window)
